//app.js

const wechat = require('./utils/wechat.js');
const utils = require("./utils/util.js");
const api = require('./api/api.js');

App({
  data: {
    version: '0.1.0'
  },
  wechat: wechat,
  utils: utils,
  api: api,
  onLaunch: function () {
    // 调用API从本地缓存中获取数据
    let that = this;
    wx.getStorage({
      key: 'isBound',
      success: (res) => {
        that.globalData.isBound = res.data;
      },
      fail: (res) => {
        //无法从本地获取到绑定信息
        //getBoundInfo()
        return
        wx.login({
          success(res) {
            api.ajaxSubmit("/node/wx/tools/haveBound", {
              code: res.code
            }).then(response => {
              let info = response.data.data;
              if (response.data.error == 10000 && info) {
                if (info.bindInfo) {
                  that.globalData.isBound = 1;
                } else {
                  that.globalData.isBound = 0;
                }
              }
              that.globalData.openId = info.openId;
              wx.setStorageSync("openId", info.openId);
              wx.setStorageSync("isBound", that.globalData.isBound);
            }).catch(err => {
              wx.showToast({
                title: "服务器开小差",
                duration: 2000,
                icon: "none",
                mask: true
              })
            })
          }
        })
      }
    })
  },
  globalData: {
    userInfo: null,
    student: null,
    canUse: false, //是否授权
    isBound: 0, //0:未绑定；1：绑定；
    openId: "",
    appId: "wx050844e5164332c5",
    appSecret: "b1de92c0ffce49a6db1f5db1f09b9773"
  },

  /**
   * 生命周期函数--监听小程序显示
   * 当小程序启动，或从后台进入前台显示，会触发 onShow
   */
  onShow() { },
  onShareAppMessage: function () { },
  /**
   * 生命周期函数--监听小程序隐藏
   * 当小程序从前台进入后台，会触发 onHide
   */
  onHide() { }
})